$(document).ready(function() {
    $("ul.sf-menu").superfish({
        autoArrows: false,
        pathClass: 'current'
    });
    $(".ft-menu li").last().css('border', 'none');
    $(".sf-menu li").last().css('background', 'none');
    function formatText(index, panel) {
        return index + "";
    }
    ;
    $(".anythingSlider").anythingSlider({
        easing: "easeInOutExpo", // Anything other than "linear" or "swing" requires the easing plugin
        autoPlay: true, // This turns off the entire FUNCTIONALY, not just if it starts running or not.
        delay: 3000, // How long between slide transitions in AutoPlay mode
        startStopped: false, // If autoPlay is on, this can force it to start stopped
        animationTime: 600, // How long the slide transition takes
        hashTags: true, // Should links change the hashtag in the URL?
        buildNavigation: true, // If true, builds and list of anchor links to link to each slide
        pauseOnHover: true, // If true, and autoPlay is enabled, the show will pause on hover
        startText: "Chạy", // Start text
        stopText: "Dừng", // Stop text
        navigationFormatter: formatText // Details at the top of the file on this use (advanced use)
    });
    $('pre code').css('overflow', 'scroll');
    $('pre code').each(function(i, block) {
        hljs.highlightBlock(block);
    });

    //Tạo thêm nút đẩy trang lên trên
    $("#toTop").scrollToTop(1000);

    $("#sltClass").change(function() {

        if ($(this).val() == -1) {
            $("#listAll").fadeOut();
            $("table#hor-minimalist-b tr").each(function(index) {
                $(this).show();
            });
        } else {
            var value = $(this).find("option:selected").text();

            $("table#hor-minimalist-b tr").each(function(index) {
                if (index === 0)
                    return;
                var id = $(this).find("td:nth-child(1)").text();
                $(this).toggle(id.indexOf(value) !== -1);
            });
            $("#listAll").fadeIn();

            $.ajax({
                type: 'POST',
                data: "class_id=" + $(this).val() + "&fromDate=&toDate=",
                url: "../Home/getListDate",
                beforeSend: function(xhr) {
                    $("#showAll table tbody").empty();
                },
                success: function(data) {
                    //alert(data.length);
                    if (data.length > 0) {
                        for (var i = 0; i < data.length; i++) {
                            var strLss = data[i].lession_name;
                            if (strLss.indexOf("#@@#@#@#@#") != -1) {
                                //alert(strLss);
                                data[i].lession_name = strLss.split("#@@#@#@#@#")[1];
                            }
                            var dateSplit = data[i].dates.split("-");
                            $("#showAll table tbody").append("<tr>" +
                                    "<td>" + dateSplit[2] + "/" + dateSplit[1] + "/" + dateSplit[0] + "</td>" +
                                    "<td>" + data[i].lession_name + "</td>" +
                                    "<td>" + data[i].theory + "</td>" +
                                    "<td>" + data[i].lab + "</td>" +
                                    "</tr>");


                        }
                    } else {
                        $("#showAll table tbody").append("<tr>" +
                                "<td>Chưa có lịch</td>" +
                                "</tr>");
                    }
                }
            });


        }



        $('#listAll a').on('click', function(e) {

            // Prevents the default action to be triggered. 
            e.preventDefault();
            // Triggering bPopup when click event is fired
            $('#showAll').bPopup({
                scrollBar: false,
                zIndex: 10,
                transitionClose: 'slideDown'
            });

        });
    });


//    $("#slide-jump").click(function() {
//        $(".anythingSlider").anythingSlider(6);
//    });
//    $('.vertical_scroller').SetScroller({velocity: 20, direction: "vertical"});
//
//    $('.choose-class').click(function() {
//        var classId = promt('Vui long nhap ID lop');
//        classId = parseInt(classId, 10);
//
//        if (classId < 1) {
//            return false;
//        }
//
//        location.href = '/codeDA/home/viewSchedule/'.classId;
});


$('marquee').css('cursor', 'pointer');
$('marquee').hover(function() {
    //this.stop();
    $(this).attr("scrollamount", 0);
}, function() {
    //this.start();
    $(this).attr("scrollamount", 2);
});

function getListTable(type) {

    var class_id = $("#sltClass").val();
    var fromDate = "";
    var nextDate = "";
    var d;
    //type =1 là next;
    if (type == 1) {
        var tmp_fromDate = $("#fromInputDate").val();
        var r = tmp_fromDate.split("-");
        d = new Date(r[0], (r[1] - 1), r[2]);
        fromDate = d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate();

        var f = new Date(d.getFullYear(), d.getMonth(), d.getDate());
        f.setDate(f.getDate() + 5);
        nextDate = f.getFullYear() + "-" + (f.getMonth() + 1) + "-" + f.getDate();

        d.setDate(d.getDate() + 7);
        $("#fromInputDate").val(d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate());
    } else {
        var tempFromDate = $("#fromInputDate").val();
        var r = tempFromDate.split("-");
        d = new Date(r[0], (r[1] - 1), r[2]);

        d.setDate(d.getDate() - 14);
        fromDate = d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate();


        var e = new Date(d.getFullYear(), d.getMonth(), d.getDate());
        e.setDate(d.getDate() + 5);
        nextDate = e.getFullYear() + "-" + (e.getMonth() + 1) + "-" + e.getDate();

        d.setDate(d.getDate() + 7);
        $("#fromInputDate").val(d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate());
    }
    //alert(fromDate + " -> " +nextDate);
    $.ajax({
        type: 'POST',
        data: "class_id=" + class_id + "&fromDate=" + fromDate + "&toDate=" + nextDate,
        url: "../Home/getListDate",
        beforeSend: function(xhr) {
            $("#hor-minimalist-b tbody").empty();
        },
        success: function(data) {
            //alert(data.length);
            for (var i = 0; i < data.length; i++) {
                var strLss = data[i].lession_name;
                if (strLss.indexOf("#@@#@#@#@#") != -1) {
                    //alert(strLss);
                    data[i].lession_name = strLss.split("#@@#@#@#@#")[1];
                }
                var dateSplit = data[i].dates.split("-");
                $("#hor-minimalist-b tbody").append("<tr>" +
                        "<td>" + data[i].classname + "</td>" +
                        "<td>" + dateSplit[2] + "/" + dateSplit[1] + "/" + dateSplit[0] + "</td>" +
                        "<td>" + data[i].lession_name + "</td>" +
                        "<td>" + data[i].theory + "</td>" +
                        "<td>" + data[i].lab + "</td>" +
                        "</tr>");
            }
        }
    });
}
